package track2.linear;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import track2.bayes.IdFeatures;
import track2.context.Config;

public class LRformula implements Serializable{
	
	private static final long serialVersionUID = 3177113645711231308L;
	public double constant = 0.034882;
	public double left;
	public double adWeight;
	public double aderWeight;
	public double qWeight;
	public double pWeight;
	public double dWeight;
	public double uWeight;
	public double tWeight;
	
//	public double getProb(double ad, double ader, double query, double purchase,
//			double des, double user, double title){
//		return constant + ad*adWeight + ader*aderWeight + query*qWeight + purchase*pWeight
//				+ des*dWeight + user*uWeight + title*tWeight;
//	}
	
	public void print(){
		System.out.println(constant + "\t" + left + "\t" + adWeight + "\t" + aderWeight + "\t" +
				qWeight + "\t" + pWeight + "\t" + dWeight + "\t" + uWeight + "\t" + tWeight);
	}

	public double getProb(boolean[] choose, double ad, double ader,
			double query, double purchase, double des, double user,
			double title) {
		double temp = 0;
		if (choose[0]) temp += left;
		if (choose[1]) temp += ad*adWeight;
		if (choose[2]) temp += ader*aderWeight;
		if (choose[3]) temp += query*qWeight;
		if (choose[4]) temp += purchase*pWeight;
		if (choose[5]) temp += des*dWeight;
		if (choose[6]) temp += user*uWeight;
		if (choose[7]) temp += title*tWeight; 
		if (temp < 0) return 0;
		else if (temp > 1) return 1;
		else return temp;
	}
	
	public static void putObject(LRformula formula) {
		try {
			File file = new File(Config.lrFormulaPath);
			FileOutputStream fs = new FileOutputStream(file);
			ObjectOutputStream os = new ObjectOutputStream(fs);
			os.writeObject(formula);
			os.flush();
			os.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static LRformula getObject() {
		LRformula formula = null;
		try {
			File file = new File(Config.lrFormulaPath);
			FileInputStream fs = new FileInputStream(file);
			ObjectInputStream os = new ObjectInputStream(fs);
			formula = (LRformula) os.readObject();
			os.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return formula;

	}

}
